package com.mrx.java8.lambda;

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

import java.util.*;


public class TestLambda {

    /** 原来的匿名内部类 */
    @Test
    public void test1(){
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
        TreeSet<Integer> ts = new TreeSet<>(comparator);
    }

    // lambda表达式
    public void test2(){
        Comparator<Integer> comparator = (x, y) -> Integer.compare(x,y);
        TreeSet<Integer> ts = new TreeSet<>(comparator);
    }


    List<Employee> employees = Arrays.asList(
        new Employee("张三",18,9999.99),
        new Employee("李四",38,5555.55),
        new Employee("王五",50,6666.66),
        new Employee("赵六",16,3333.33),
        new Employee("田七",8,7777.77)
    );

    // 需求：获取当前公司中员工年龄大于35的员工信息
    public List<Employee> filterEmployees(List<Employee> list){
        List<Employee> emps = new ArrayList<>();
        for(Employee emp : list){
            if(emp.getAge() >= 35){
                emps.add(emp);
            }
        }
        return emps;
    }

    // 需求：获取当前公司中员工工资大于5000的员工信息
    public List<Employee> filterEmployees2(List<Employee> list){
        List<Employee> emps = new ArrayList<>();
        for(Employee emp : list){
            if(emp.getSalary()>= 5000){
                emps.add(emp);
            }
        }
        return emps;
    }

    @Test
    public void test3_1(){
        List<Employee> list =  filterEmployees(employees);
        for (Employee emp : list){
            System.out.println(emp);
        }
    }

    @Test
    public void test3_2(){
        List<Employee> list =  filterEmployees2(employees);
        for(Employee emp : list){
            System.out.println(emp);
        }
    }



    // 优化方式一:策略设计模式
    public List<Employee> filterEmployee(List<Employee> list,MyPredicate<Employee> mp){
        List<Employee> emps = new ArrayList<>();
        for(Employee emp : list){
            if (mp.test(emp)){
                emps.add(emp);
            }
        }
        return emps;
    }

    @Test
    public void test4_1(){
        List<Employee> emps = filterEmployee(employees,new FilterEmployeeByAge());
        System.out.println(JSON.toJSONString(emps));
    }

    @Test
    public void test4_2(){
        List<Employee> emps = filterEmployee(employees,new FilterEmployeeBySalary());
        System.out.println(JSON.toJSONString(emps));
    }

    // 优化方式二:匿名内部类
    @Test
    public void test5(){
        List<Employee> list = filterEmployee(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary()<=5000;
            }
        });

        for(Employee emp : list){
            System.out.println(emp);
        }
    }

    // 优化方式三:lambda表达式
    @Test
    public void test6(){
        List<Employee> list = filterEmployee(employees,employee -> employee.getSalary()>=5000);
        list.forEach(System.out::println);
    }

    // 优化方式四：stream API
    @Test
    public void test7(){
        employees.stream()
                .filter(employee -> employee.getSalary()>=5000)
                .limit(2)
                .forEach(System.out::println);
        System.out.println("------------------------------------------");
        employees.stream()
                .map(employee -> employee.getName())
                .forEach(System.out::println);
    }








}
